package com.tianqu.tms.www.car.action;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.tianqu.tms.core.base.Logger;
import com.tianqu.tms.core.base.TmsAction;
import com.tianqu.tms.core.base.UserContext;
import com.tianqu.tms.core.util.Registry;
import com.tianqu.tms.core.util.TmsConstants;
import com.tianqu.tms.entity.Codes;
import com.tianqu.tms.entity.Member;
import com.tianqu.tms.entity.Vendor;
import com.tianqu.tms.www.car.viewhelper.CarCreationInputViewHelper;



/**
 * @author zhangxueliang
 * 车辆信息录入Action
 */
public class CarCreationInputAction extends TmsAction{

	private Logger logger = Logger.getLogger(CarCreationInputAction.class);
	private static final String BACKFLAG = "backFlag"; 
	@Override
	protected ActionForward doAction(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response,
			UserContext uc) throws Exception {
		logger.debug(this.getClass().getName() + "#execute was called.");		
		
		CarCreationInputViewHelper carCreationInputViewHelper = new CarCreationInputViewHelper();
		//this.saveToken(request);
		Codes code = Codes.getInstance();
		carCreationInputViewHelper.setCarTypeList(code
				.getCodeslistByItemId(Codes.VEHICLE_TYPE));// 车辆类型
		carCreationInputViewHelper.setColorList(code
				.getCodeslistByItemId(Codes.VEHICLE_COLOR));// 车辆颜色
		carCreationInputViewHelper.setCarUseList(code
				.getCodeslistByItemId(Codes.USER_NATURE));// 使用性质list
		carCreationInputViewHelper.setCardTypeList(code
				.getCodeslistByItemId(Codes.PLATE_TYPE));// 号牌种类list
		carCreationInputViewHelper.setFuelTypeList(code
				.getCodeslistByItemId(Codes.FUEL_TYPE));// 燃料种类list
		carCreationInputViewHelper.setCountryStyleList(code
				.getCodeslistByItemId(Codes.VEHICLE_COUNTRY));// 国别list
		carCreationInputViewHelper.setAttributeList(code
				.getCodeslistByItemId(Codes.ATTACHMENT_PROPERTY));// 车辆状态list'
		carCreationInputViewHelper.setOwnerTypeList(code
				.getCodeslistByItemId(Codes.CAR_OWNER));// 车主类型list
		Member menber = Member.getInstance();
		carCreationInputViewHelper.setMemberList(menber.findAllMemInfo());// 会员类型
		// 车辆品牌
		Vendor vendor = Vendor.getInstance();
		//carCreationInputViewHelper
				//.setVendorList(vendor.findAllVendorInfo());// 车主类型list
		carCreationInputViewHelper.setGearBoxList(code.getCodeslistByItemId(Codes.GEARBOX_TYPE));//变速箱类型list
		String lPlate = Registry.getConfig(TmsConstants.TMS_DEFLICENSEPLATE_KEY);//车牌前两位
		carCreationInputViewHelper.setLicensePlate(lPlate);
		carCreationInputViewHelper.setOwnerTypeFlag(2);
		if (uc != null) {
			if(uc.getAttribute(BACKFLAG) != null){//判断保存是否成功，
				int flag = (Integer)uc.getAttribute(BACKFLAG);
				if(flag == 1){
					carCreationInputViewHelper.setBackFlag(1);
				}
			}
			uc.clear();			
		}
		uc.setAttribute(TmsConstants.VIEWHELP, carCreationInputViewHelper);
		request.setAttribute(VIEWHELP, carCreationInputViewHelper);
		logger.debug(this.getClass().getName() + "#execute was completed.");
		return mapping.findForward(TmsConstants.SUCCESS);
	}

}
